//
//  JincaItems.m
//  qulvyou
//
//  Created by mmyy on 15/10/22.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "JincaItems.h"

#import "CoreData+MagicalRecord.h"

@implementation JincaItems
@dynamic cover_image;
@dynamic day_count;
@dynamic first_day;
@dynamic vid;
@dynamic name;
@dynamic avatar_l;
@dynamic uid;
@dynamic uname;
@dynamic view_count;
@dynamic popular_place_str;

+(instancetype)jincaItemsWithDict:(NSDictionary *)item
{
    NSDictionary *dict = [[item objectForKey:@"data"] firstObject];
    if (![[dict objectForKey:@"user"] isKindOfClass:[NSNull class]] && ![[[dict objectForKey:@"user"] objectForKey:@"name"] isEqualToString:@"台北٩(˃̶͈̀௰˂̶͈́)و懒蛋的七日旅行"]) {
        JincaItems *itmes = [JincaItems MR_createEntity];
        itmes.cover_image = dict[@"cover_image"];
        itmes.day_count = [dict[@"day_count"] stringValue];
        if (![dict[@"first_day"] isKindOfClass:[NSNull class]]) {
            itmes.first_day =  [NSString stringWithFormat:@"%@", dict[@"first_day"]];
        }
        
        itmes.vid = [dict[@"id"] stringValue];
        itmes.name = dict[@"name"];
        itmes.avatar_l  = [[dict objectForKey:@"user"] objectForKey:@"avatar_l"];
        itmes.uid = [[[dict objectForKey:@"user"] objectForKey:@"id"] stringValue];
        itmes.uname = [[dict objectForKey:@"user"] objectForKey:@"name"];
        itmes.view_count = [dict[@"view_count"] stringValue];
        itmes.popular_place_str = dict[@"popular_place_str"];
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        return itmes;
    }else return nil;
}

+(NSArray *)getAllData
{
    return [JincaItems MR_findAll];
    
}

+(void)removeAllData
{
    NSArray *array = [JincaItems MR_findAll];
    for (JincaItems *items  in array) {
        [items MR_deleteEntity];
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    }
}
@end
